package com.study.common;

import com.alibaba.fastjson.JSON;

public class ResponseMessage {

    // 响应业务状态
    private Integer code;

    // 响应消息
    private String message;

    // 响应中的数据
    private Object data;

    public ResponseMessage() {
    }
    public ResponseMessage(Object data) {
        this.code = 200;
        this.message = "OK";
        this.data = data;
    }
    public ResponseMessage(String message, Object data) {
        this.code = 200;
        this.message = message;
        this.data = data;
    }

    public ResponseMessage(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static ResponseMessage ok() {
        return new ResponseMessage(null);
    }
    public static ResponseMessage ok(String message) {
        return new ResponseMessage(message, null);
    }
    public static ResponseMessage ok(Object data) {
        return new ResponseMessage(data);
    }
    public static ResponseMessage ok(String message, Object data) {
        return new ResponseMessage(message, data);
    }

    public static ResponseMessage build(Integer code, String message) {
        return new ResponseMessage(code, message, null);
    }

    public static ResponseMessage build(Integer code, String message, Object data) {
        return new ResponseMessage(code, message, data);
    }

    public String toJsonString() {
        return JSON.toJSONString(this);
    }


    /**
     * JSON字符串转成 ResponseMessage 对象
     * @param json
     * @return
     */
    public static ResponseMessage format(String json) {
        try {
            return JSON.parseObject(json, ResponseMessage.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
